package org.shunfeng.dao.impl;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.shunfeng.dao.DeliveryListDao;
import org.shunfeng.pojo.Delivery;
import org.shunfeng.utils.DruidUtils;

import java.sql.SQLException;
import java.util.List;

public class DeliveryListDaoImpl implements DeliveryListDao {
    private QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
    @Override
    public List<Delivery> querydeliveryListByUserId(Integer userId) throws SQLException {
        String sql = "select id as id, delivery_name as deliveryName, user_id as userId,\n" +
                "       company_id as companyId, phone as phone, address as address,\n" +
                "       send_time as sendTime, state as state  from sys_delivery where user_id = ?";

        return qr.query(sql, new BeanListHandler<Delivery>(Delivery.class), userId);
    }

    @Override
    public int addDelivery(String deliveryName, String phone, String address, String sendTime, Integer companyId, Integer userId, String state) throws SQLException {
        String sql = "insert into sys_delivery (delivery_name, user_id, company_id, phone, address, send_time, state)values(?,?,?,?,?,?,?)";
        return qr.update(sql, deliveryName, userId, companyId, phone, address, sendTime, state);
    }
    @Override
    public int deleteDelivery(String Id) throws SQLException {
        String sql = "delete from sys_delivery where id = ?";
        return qr.update(sql, Id);
    }

    @Override
    public Delivery queryUserByDeliveryId(Integer id) throws SQLException{
        String  sql = "select id, delivery_name as deliveryName, user_id as userId, company_id as CompanyId, phone, address, send_time as sendTime, state  from sys_delivery where id = ?";
        return qr.query(sql, new BeanHandler<Delivery>(Delivery.class), id);
    }
    @Override
    public int updateDelivery(String deliveryName, String phone, String address, String sendTime, Integer companyId, Integer userId, String state, String deliveryId) throws SQLException{
        String sql = "update sys_delivery set delivery_name = ?, phone = ?, address = ?, send_time = ?, user_id = ?, company_id = ?, state = ? where id = ?";
        return qr.update(sql, deliveryName, phone, address, sendTime, companyId, userId, state, deliveryId);
    }
}
